home *** CD-ROM | disk | FTP | other *** search
-
- /* ScoresDemo */
-
- /* by Ingemar Ragnemalm 1995 */
-
- /* This demo shows how to save settings to a preference file, how to display
- a dialog asking for the player's name, and how to display a high score list */
-
-
- #include "MiniPreferences.h"
-
-
- #define abs(x) (x>0?x:-x)
-
-
- #define kOKButton 1
- #define kCancelButton 2
-
-
- /****************** A dialog asking for the player's name ******************/
-
- /* StdFilter */
-
- /* StdFilter is a dialog filter function that is useful for most
- modal dialogs, as long as they have an OK and a Cancel button.
- It maps return and enter to OK, and command-period and ESC to Cancel.
- It also draws a frame around the OK button. */
-
- static pascal Boolean StdFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
- {
- char theChar;
- short itemKind;
- Handle itemHandle;
- Rect itemBox;
-
- switch ( theEvent->what )
- {
- case keyDown:
- theChar = (char)(theEvent->message & charCodeMask);
- if ( (((theEvent->modifiers & cmdKey) != 0) && (theChar == '.')) || (theChar == (char)27) ) /*cmd-. or ESC*/
- {
- *itemHit = kCancelButton;
- /*Highlight the cancel button*/
- GetDItem(theDialog, kCancelButton, &itemKind, &itemHandle, &itemBox);
- HiliteControl((ControlHandle)itemHandle, 1);
-
- return true;
- };
- if ( (theChar == (char)13) || (theChar == (char)3) )
- {
- *itemHit = kOKButton;
- /*Highlight the OK button*/
- GetDItem(theDialog, 1, &itemKind, &itemHandle, &itemBox);
- HiliteControl((ControlHandle)itemHandle, kOKButton);
-
- return true;
- };
- break; /*keyDown*/
- case updateEvt:
- BeginUpdate(theDialog);
- SetPort(theDialog);
-
- DrawDialog(theDialog);
-
- /*Frame default button - item 1*/
- GetDItem(theDialog, kOKButton, &itemKind, &itemHandle, &itemBox);
- InsetRect(&itemBox, -4, -4);
- PenSize(3, 3);
- FrameRoundRect(&itemBox, 15, 15);
-
- EndUpdate(theDialog);
- break; /*update event*/
- }; /*case*/
- return false;
- } /*StdFilter*/
-
-
- /* Copy a Pascal-string */
- static void CopyString(Str255 dest, Str255 src)
- {
- short i;
-
- for (i=0; i <= src[0]; i++) dest[i] = src[i];
- } /* CopyString */
-
-
-
- #define kHighDlogRes 128
-
- /* Ask for players name (at highscore) */
- static void AskHigh(Str255 name)
- {
- DialogPtr dialog;
- GrafPtr oldPort;
- short itemHit;
- Handle itemHandle;
- short itemType;
- Rect itemRect;
-
- GetPort(&oldPort);
- dialog = GetNewDialog(kHighDlogRes, nil, (WindowPtr)-1);
- ShowWindow(dialog);
- SelectWindow(dialog);
- SetPort(dialog);
-
- GetDialogItem(dialog, 3, &itemType, &itemHandle, &itemRect);
- SetDialogItemText(itemHandle, "\pYour name here"); /*Insert string from the prefs file here*/
- SelectDialogItemText(dialog, 3, 0, 32767);
- itemHit = -1;
- while ( (itemHit != 1) && (itemHit != 2) ) /* 1=ok, 2=cancel */
- ModalDialog(&StdFilter, &itemHit);
- if ( itemHit == 2 )
- {
- name[0] = 0; /* Empty string */
- };
- if ( itemHit == 1 )
- {
- GetDialogItem(dialog, 3, &itemType, &itemHandle, &itemRect);
- GetDialogItemText(itemHandle, name);
- };
- SetPort(oldPort);
- DisposeDialog(dialog);
- } /*AskHigh*/
-
-
- /****************** High score handling ******************/
-
- #define kNameLength 15 /* Must match the length of the name string type */
-
- static short gLastHigh;
-
- /* Highscore record type */
-
- typedef struct
- {
- long score[11];
- Str15 name[11];
- } HighScoreRec;
- typedef HighScoreRec **HighScoreHnd;
-
- /* Global handle to the high score resource */
- HighScoreHnd gHighScoreHandle;
-
- /* Call this on game over! */
- static void UpdateHighScore(long score)
- {
- short rank;
- Str255 name;
-
- if ( score > (**gHighScoreHandle).score[10] )
- {
- rank = 10;
- /*Call some function that asks for the name.*/
- AskHigh(name);
- /*Max kNameLength characters! We take some extra trouble to append '…' too.*/
- if ( name[0] > kNameLength )
- name[0] = kNameLength;
- if ( name[0] == 0 ) /* length of name = 0 */
- return;
- /*Decrement rank until we get to a position where the one above is better!*/
- while ( ((**gHighScoreHandle).score[rank - 1] < score) && (rank > 1) )
- {
- (**gHighScoreHandle).score[rank] = gHighScoreHandle[0]->score[rank - 1];
- CopyString( (**gHighScoreHandle).name[rank] , gHighScoreHandle[0]->name[rank - 1]);
- rank--;
- };
- /*Remember last high for the highscore display*/
- gLastHigh = rank;
- /*Write in the score in the proper place and save*/
- (**gHighScoreHandle).score[rank] = score;
- CopyString( (**gHighScoreHandle).name[rank] , name);
- ChangedResource((Handle)gHighScoreHandle);
- }
- } /*UpdateHighScore*/
-
-
- /*InitScores*/
- /*This procedure loads the high score list from a resource in the current resource file,*/
- /*creating a new one if there is none.*/
-
- static void InitScores()
- {
- short rank;
-
- gHighScoreHandle = (HighScoreHnd)GetResource('Bäst', 0);
- if ( gHighScoreHandle == nil ) /*Didn't exist - create it!*/
- {
- gHighScoreHandle = (HighScoreHnd)NewHandle(sizeof(HighScoreRec));
- if ( gHighScoreHandle == nil )
- ExitToShell(); /*Insert error message here*/
- for ( rank = 1 ; rank < 10; rank++ )
- {
- (**gHighScoreHandle).score[rank] = 0;
- CopyString( (**gHighScoreHandle).name[rank] , "\pNobody");
- };
- AddResource((Handle)gHighScoreHandle, 'Bäst', 0, "\pHigh scores");
- }
- else /*Did exist - check the size!*/
- if ( GetHandleSize((Handle)gHighScoreHandle) < sizeof(HighScoreRec) )
- SetHandleSize((Handle)gHighScoreHandle, sizeof(HighScoreRec));
- } /*InitScores*/
-
-
- /* Standard inits */
-
- static void InitToolbox(void) {
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent,0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (nil);
- InitCursor ();
- }
-
- /****************** Main program ******************/
-
- void main(void) {
- WindowPtr myWindow;
- Str255 tempString;
- Rect windowRectangle;
- short score, rank;
- short appFile, prefFile;
-
- InitToolbox();
-
- /*Get the prefs file - create as necessary.*/
- if ( SetPrefFile("\pScoresDemo Preferences", '????', 'pref', &appFile, &prefFile) )
- ;
-
- /*Set the current resource file to the preference file before InitScores*/
- if ( prefFile != 0 )
- UseResFile(prefFile);
- InitScores();
- UseResFile(appFile);
-
- /*Let's seed the random number generator so we don't get the same all the time!*/
- qd.randSeed = TickCount ();
-
- /*Update the high score list with a random score.*/
- score = Random();
- UpdateHighScore(abs(score));
-
- /*Set up the window*/
- SetRect(&windowRectangle, 100, 100, 400, 260);
- myWindow = NewCWindow(nil, &windowRectangle, "\pHigh scores demo", true, 0, (WindowPtr)-1L, false, 0);
- SetPort(myWindow);
-
- for ( rank = 1 ; rank < 10; rank++)
- {
-
- /*We draw the latest high score with red.*/
- if ( rank == gLastHigh )
- ForeColor(redColor);
- else
- ForeColor(blackColor);
-
- /*Draw the position number*/
- MoveTo(10, 15 * rank);
- NumToString(rank, tempString);
- DrawString(tempString);
-
- /*Draw the name*/
- MoveTo(50, 15 * rank);
- DrawString((**gHighScoreHandle).name[rank]);
-
- /*Draw the score*/
- MoveTo(200, 15 * rank);
- NumToString((**gHighScoreHandle).score[rank], tempString);
- DrawString(tempString);
- };
-
- while ( ! Button () )
- ;
- } /*ScoresDemo*/
-